/**
 * @author Tobias Boehm
 *
 * 06.05.2011
 */
package org.anddev.andengine.extension.tbsengine.message.client;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
import java.util.Vector;

import org.anddev.andengine.extension.tbsengine.message.ICopyable;
import org.anddev.andengine.extension.tbsengine.player.BasePlayer;

/**
 * This is a client chat message. It contains the source, the {@link #mMessage}
 * a vector of recipients ({@link #mReceivingPlayerIDs}) and (optional) a {@link #mMatchID}
 * to assign the chat message to a running match.
 * 
 * @author Tobias Boehm
 *
 */
public class ClientChatmessageMessage extends AbsClientMessage implements ICopyable<ClientChatmessageMessage> {

	private Vector<UUID> mReceivingPlayerIDs;
	private String mMessage = "";
	private UUID mMatchID;

	
	/* (non-Javadoc)
	 * @see org.anddev.andengine.extension.multiplayer.protocol.adt.message.IMessage#getFlag()
	 */
	@Override
	public short getFlag() {
	
		return FLAG_CLIENT_CHATMESSAGE;
	}

	/**
	 * @return the mMessage
	 */
	public String getMessage() {
		return mMessage;
	}

	/**
	 * @param mMessage the mMessage to set
	 */
	public void setMessage(String mMessage) {
		this.mMessage = mMessage;
	}
	
	public void setMatchID(final UUID pMatchID){
		
		this.mMatchID = pMatchID;
	}


	/**
	 * @return the mMatchID
	 */
	public UUID getMatchID() {
		return mMatchID;
	}
	
	/**
	 * @return the mReceivingPlayerIDs
	 */
	public Vector<UUID> getReceivingPlayerIDs() {
		return mReceivingPlayerIDs;
	}
	
	protected void initRecipientVector(){
				
		this.mReceivingPlayerIDs = new Vector<UUID>();
	}
	
	public void setRecipients(final List<BasePlayer> pRecipients){
	
		this.initRecipientVector();
		
		for(BasePlayer recipient : pRecipients){
			
			this.mReceivingPlayerIDs.add(recipient.getPlayerID());
		}
		
	}

	@Override
	protected void onReadTransmissionClientData(DataInputStream pDataInputStream)
			throws IOException {
		
		this.mMessage = pDataInputStream.readUTF();
		
		final int receiverCount = pDataInputStream.readInt();
		
		this.initRecipientVector();
		
		for(int receiverIDpos = 0; receiverIDpos < receiverCount; receiverIDpos++){
			
			this.mReceivingPlayerIDs.add(UUID.fromString(pDataInputStream.readUTF()));
		}
	}

	@Override
	protected void onWriteTransmissionClientData(
			DataOutputStream pDataOutputStream) throws IOException {
		
		pDataOutputStream.writeUTF(this.mMessage);
		
		pDataOutputStream.writeInt(this.mReceivingPlayerIDs.size());
		
		for(UUID receiverID : this.mReceivingPlayerIDs){
			
			pDataOutputStream.writeUTF(receiverID.toString());
		}
	}

	@Override
	public ClientChatmessageMessage copy() {
		
		final ClientChatmessageMessage copy = new ClientChatmessageMessage();
		
		copy.initRecipientVector();
		
		copy.mMessage = this.mMessage;
		copy.mPlayerID = this.mPlayerID;
		copy.mReceivingPlayerIDs.addAll(this.mReceivingPlayerIDs);
		copy.mMatchID = this.mMatchID;
		
		return copy;
	}

}
